<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/11/19
 * Time: 下午8:12
 */

class ModServerChannel extends Model {

    protected $table = 'server_channel';

    // 表名
    public function tableName(){
        return $this->table;
    }

    public static function model($className=__CLASS__){
        return parent::model($className);
    }

    public function getDbConnection(){
        return Yii::app()->dbOss;
    }

    public function addServerChannel($params){
    	$result = $this->getDbConnection()->multiInsert($params, $this->table, FALSE);
    	return $result;
    }
    
    public function select($params){
    	$where  = ' WHERE server_id=:server_id';
    	$args   = array(
    			':server_id'     => $params['server_id']
    	);
    	$sql = "SELECT * FROM {$this->table} {$where} ";
    
    	$result = $this->getDbConnection()->getAll($sql, $args);
    	return $result;
    }
    
    public function getSelectServer($params){
    	$where  = ' WHERE channel_id=:channel_id';
    	$args   = array(
    			':channel_id'     => $params['channel_id']
    	);
    	$sql = "SELECT * FROM {$this->table} {$where} ";
    
    	$result = $this->getDbConnection()->getAll($sql, $args);
    	return $result;
    }
    
    public function selectChannel($server_ids,$game){
    	$sql = "select ac.server_id,ac.channel_id,c.channel_name,c.tab from server_channel as ac INNER JOIN channel as c on ac.channel_id = c.id WHERE ac.server_id IN (".$server_ids.") and c.game = '".$game."' ";
    	$result = $this->getDbConnection()->query($sql);
    	return $result;
    }
    
    public function selectServel($channel_ids,$game){
    	$sql = "select ac.server_id,ac.channel_id,s.server_name,s.server_num FROM server_channel as ac INNER JOIN server as s on ac.server_id = s.id WHERE ac.channel_id IN (".$channel_ids.") and s.game = '".$game."' ";
    	$result = $this->getDbConnection()->query($sql);
    	return $result;
    }
    
    /**
     * 同步渠道服务器所用 1版本
     * @param unknown $channel_id
     * @param unknown $game
     * @return unknown
     */
    public function selectServerForChannel($channel_id,$game){
    	$sql = "select ac.server_id,ac.channel_id,s.develop_id,s.name,s.server_name,c.tab FROM server_channel as ac INNER JOIN server as s on ac.server_id = s.id INNER JOIN channel as c on c.id = ac.channel_id ";
    	$sql .= "WHERE ac.channel_id = $channel_id and s.game = '".$game."' order by s.sort_num asc ";
    	$result = $this->getDbConnection()->query($sql);
    	return $result;

    }
    
    /**
     * 同步渠道服务器所用 2版本
     * 根据渠道筛选出来的服务器（所属的专服的同步URL需要一样）
     * @param unknown $channel_id
     * @param unknown $game
     * @param unknown $api_synchro_url
     * @return unknown
     */
    public function selectServerForChannel1($channel_id,$game,$api_synchro_url){
    	$sql = "select ac.server_id,ac.channel_id,s.develop_id,s.name,s.server_name,c.tab FROM server_channel as ac INNER JOIN server as s on ac.server_id = s.id INNER JOIN channel as c on c.id = ac.channel_id ";
    	$sql .= " INNER JOIN special_servicer as spes ON spes.server_id = s.id INNER JOIN special_server as sse ON sse.id = spes.special_id ";
    	$sql .= "WHERE ac.channel_id = $channel_id and s.game = '".$game."' and sse.api_synchro_url = '".$api_synchro_url."' order by s.sort_num asc ";
    	//echo $sql;
    	$result = $this->getDbConnection()->query($sql);
    	return $result;
    
    }
    
    public function selectServerForChannelTab($tab,$game){
    	$sql = "select ac.server_id,ac.channel_id,s.develop_id,s.server_name,c.tab FROM server_channel as ac INNER JOIN server as s on ac.server_id = s.id INNER JOIN channel as c on c.id = ac.channel_id ";
    	$sql .= "WHERE c.tab = $tab and s.game = '".$game."' ";
    	$result = $this->getDbConnection()->query($sql);
    	return $result;
    
    }
    
    /**
     * 用于 RoutineCrossServerService->fitServer
     * @param unknown $server_ids
     * @param unknown $game
     * @return unknown
     */
    public function selectServerChannel($server_ids,$game){
    	$sql = "select ac.server_id,ac.channel_id,c.channel_name,c.tab,s.check_flag from server_channel as ac INNER JOIN channel as c on ac.channel_id = c.id ";
    	$sql .= " INNER JOIN server as s on s.id = ac.server_id ";
    	$sql .= " WHERE ac.server_id IN (".$server_ids.") and c.game = '".$game."' ";
    	$result = $this->getDbConnection()->query($sql);
    	return $result;
    }
    

} 